/**
 * Created by chenzheyu on 2017/4/28.
 */


var CarIds = window.id;
$(function () {
    var CarId = CarIds;
    var limit = 10;
    var companyId = '';
    var companyIds = '';
    var companyName = '';
    var CarName = '';
    var vehicleId = '';
    var CarLimitId = '';
    var CarLimitName = '';
    var ShopId = '';
    var ShopIds = '';
    var ShopName = '';
    var CarSeriesId = '';
    var CarSeriesName = '';
    var CarBrandId = '';
    var CarBrandName = '';
    var purchaseTime = '';
    var plateNo = '';
    var mileage = '';


    $.ajax({
        url: ctx + '/rentcar/findById',
        data: {
            id: CarId
        },
        success: function (data) {
            var data = eval('(' + data.data + ')');


            var LimitDownPullSelectOption = {
                data: [
                    {name: '不限行', Val: 0},
                    {name: '星期一', Val: 1},
                    {name: '星期二', Val: 2},
                    {name: '星期三', Val: 3},
                    {name: '星期四', Val: 4},
                    {name: '星期五', Val: 5},
                    {name: '星期六', Val: 6},
                    {name: '星期日', Val: 7}
                ],
                onChange: function (e) {
                    CarLimitId = e.val;
                    CarLimitName = e.text;
                }
            };
            $('.limit').DownPullSelect(LimitDownPullSelectOption);

            $('.limit select').val(data.rentCar.limit);
            $('.limit select').change();

            $('.CarName input').val(data.rentCar.carName);
            $('.mileage input').val(data.car.mileage);
            companyIds = data.rentCar.companyId;
            $('.purchase input').val($().Timestamp.TimestampDate(data.rentCar.purchase));
            ShopIds = data.rentCar.shopId;
            $('.limit select').val(data.rentCar.limit);
            $('.plateNo input').val(data.car.plateNo);
            vehicleId = data.vehicle.id;
            $('.vehicleId input ').val(data.vehicle.name);
            purchaseTime = data.rentCar.purchase;

            $.ajax({
                url: ctx + '/rentvehicle/findById',
                data: {
                    id: data.vehicle.id
                },
                success: function (data) {
                    $('.vehicleId input').val(data.data.name)
                }
            });


            var timestamp = Date.parse(new Date())/1000;
            $('.purchase input ').datetimepicker({
                format: "yyyy-mm-dd HH:ii",
                language: 'zh-CN',
                showMeridian: true,
                autoclose: true,
                todayBtn: true,
                endDate: $().Timestamp.TimestampDate(timestamp)
            });
            $('.purchase input ').on('change', function () {

                var Titme = $(this).val();
                purchaseTime = $().Timestamp.DateTimestamp(Titme);
            });


            var init=true;
            $.ajax({
                url:ctx+'/company/listAllData',
                type: 'GET',
                async:false,
                success: function (data) {

                    var DownPullSelectOption = {
                        data: [],
                        onChange: function (e) {

                            companyId = e.val;
                            companyName = e.text;

                            $.ajax({
                                url: ctx + '/rentshop/selectRentshopByShopName',
                                type: 'GET',
                                async:false,
                                data: {
                                    companyId: companyId
                                },
                                success: function (data) {
                                    ;
                                    var ShopNameDownPullSelectOption = {
                                        data: [],
                                        onChange: function (e) {

                                            ShopId = e.val;
                                            ShopName = e.text;


                                            $(".vehicleId input").unbind("click");
                                            $('.vehicleId input').on('click', function () {
                                                var vehicleIdOption = {
                                                    data: {
                                                        Title: '选择车系',
                                                        Content: '',
                                                        OkChange: function (e, Remove) {
                                                            if (!CarSeriesId == '' && !CarBrandId == '' && !CarSeriesName == '' && !CarBrandName == '') {
                                                                var data = {
                                                                    vehicleId: CarSeriesId,
                                                                };
                                                                $('.vehicleId input').data('data', data).val(CarSeriesName);
                                                                Remove()
                                                            } else {
                                                                $('body').Overlays({
                                                                    data: {
                                                                        text: '请选择一个车系！',
                                                                        state: 'No'
                                                                    }
                                                                })
                                                            }
                                                        }
                                                    },
                                                    OKHide: true
                                                };

                                                var vehicleIdContent = $('<div class="vehicleIdContent"></div>');


                                                var pids = null
                                                $.ajax({
                                                    url: ctx + '/rentcontent/selectAllProperty',
                                                    type: 'GET',
                                                    async: false,
                                                    success: function (data) {
                                                        for (var i = 0; i < data.length; i++) {
                                                            if (data[i].seq === 4) {
                                                                pids= data[i].id
                                                            }
                                                        }
                                                    }
                                                });

                                                $.ajax({
                                                    url: ctx + '/rentcontent/selectContentByPid',
                                                    data: {
                                                        pid: pids
                                                    },
                                                    type: 'GET',
                                                    success: function (data) {
                                                        if (data.length > 0) {
                                                            for (var i = 0; i < data.length; i++) {
                                                                (function () {
                                                                    var index = i;
                                                                    var val = $('<div class="Brand"></div>').html('<div class="BrandName">' + data[index].contentName + '</div><div class="Vehicle CustomClear"></div>');
                                                                    var Id = data[index].id;
                                                                    var Name = data[index].contentName;
                                                                    $.ajax({
                                                                        url: ctx + '/rentvehicle/findVehicleByparams',
                                                                        data: {
                                                                            shopId: ShopId,
                                                                            brandId: data[index].id
                                                                        },
                                                                        type: 'GET',
                                                                        success: function (data) {
                                                                            ;
                                                                            for (var i = 0; i < data.content.length; i++) {
                                                                                (function () {
                                                                                    var index = i;
                                                                                    var item = $('<label class="CustomClear"></label>');
                                                                                    item.append('<input type="radio" name="Vehicle" value="' + data.content[i].id + '"><span>' + data.content[i].name + '</span>');
                                                                                    item.on('click', function () {
                                                                                        vehicleId = data.content[index].id;
                                                                                        CarSeriesId = data.content[index].id;
                                                                                        CarSeriesName = data.content[index].name;
                                                                                        CarBrandId = Id;
                                                                                        CarBrandName = Name;
                                                                                    });
                                                                                    val.find('.Vehicle').append(item)
                                                                                })()
                                                                            }

                                                                        }
                                                                    });
                                                                    vehicleIdContent.append(val)
                                                                })()
                                                            }
                                                        }
                                                    }
                                                });

                                                vehicleIdOption.data.Content = vehicleIdContent;


                                                $('body').ModalBox(vehicleIdOption);

                                            });


                                        }
                                    };
                                    if (data.content.length > 0) {
                                        for (var i = 0; i < data.content.length; i++) {
                                            (function () {
                                                var index = i;
                                                var val = {
                                                    name: data.content[index].shopName,
                                                    Val: data.content[index].id
                                                };
                                                ShopNameDownPullSelectOption.data.push(val)
                                            })()
                                        }


                                    } else {
                                        ShopNameDownPullSelectOption.data.push({name: '该公司下无门店'})
                                    }


                                    $('.shopName').DownPullSelect(ShopNameDownPullSelectOption);

                                    if(init){
                                        init=false;
                                        $('.shopName select').val(ShopIds);
                                        $('.shopName select').change();
                                    }else if(data.content.length>1){
                                        $('.shopName select').val(data.content[0].id);
                                        $('.shopName select').change();
                                    }

                                }
                            });



                        }
                    };
                    for (var i = 0; i < data.length; i++) {
                        (function () {
                            var index = i;
                            DownPullSelectOption.data.push({
                                name: data[index].abbreviation,
                                Val: data[index].id
                            });
                        })()
                    }

                    $('.companyName').DownPullSelect(DownPullSelectOption);
                    $('.companyName select').val(companyIds);
                    $('.companyName select').change()
                }
            });


            $('.plateNo input').on('blur', function () {
                if ($().isVehicleNumber($(this).val())) {
                    $.ajax({
                        url: ctx + '/rentcar/findCar',
                        data: {
                            param: $(this).val()
                        },
                        type: 'GET',
                        success: function (data) {
                            if (!data.totalElements == 0) {
                                $('body').Overlays({data: {text: "当前车牌号已存在，请重新输入！", state: 'No'}});
                                $('.plateNo input').val('')
                            }
                        }
                    })
                } else {
                    $('body').Overlays({data: {text: "请输入正确的车牌号！", state: 'No'}});
                    $('.plateNo input').val('')
                }

            });


            $('.OkBut').on('click', function () {

                if ($('.CarName input').val()) {
                    CarName = $('.CarName input').val();
                    if ($('.mileage input').val()) {
                        mileage = $('.mileage input').val();
                        if (!companyId == '' && !companyName == '') {
                            if ($('.purchase input').val()) {
                                if (!ShopId == '' && !ShopName == '') {
                                    if (!CarLimitId == '' && !CarLimitName == '') {
                                        if ($('.plateNo input').val()) {
                                            plateNo = $('.plateNo input').val();
                                            if (!vehicleId == '') {

                                                var data = {
                                                    id: CarId,
                                                    name: CarName,//车辆名称
                                                    companyName: companyName,//公司名称
                                                    companyId: companyId,//公司ID
                                                    shopId: ShopId,//门店ID
                                                    shopName: ShopName,//门店名称
                                                    plateNo: plateNo,//车牌号
                                                    mileage: mileage,//已行驶里程
                                                    limit: CarLimitId,//是否限行 0不限行 1-7对应星期
                                                    purchase: purchaseTime,//购置时间
                                                    vehicleId: vehicleId//车系ID
                                                };


                                                $.ajax({
                                                    url: ctx + '/rentcar/updateCar',
                                                    data: data,
                                                    type: 'POST',
                                                    success: function (data) {
                                                        if (data.message == '车辆修改成功') {
                                                            $('body').Overlays({
                                                                data: {
                                                                    text: '车辆修改成功，即将跳转页面！',
                                                                    state: "Ok"
                                                                }
                                                            });

                                                            setInterval(function () {
                                                                window.location.href=ctx+'/rentcar/list'
                                                            },2000)
                                                        } else {
                                                            $('body').Overlays({
                                                                data: {text: '车辆修改失败，请稍后重试！'},
                                                                state: 'No'
                                                            });
                                                        }
                                                    }
                                                });


                                            } else {
                                                $('body').Overlays({data: {text: '请选择车系！', state: "No"}})
                                            }
                                        } else {
                                            $('body').Overlays({data: {text: '请输入车牌号！', state: "No"}})
                                        }
                                    } else {
                                        $('body').Overlays({data: {text: '请选择是否限行！', state: "No"}})
                                    }
                                } else {
                                    $('body').Overlays({data: {text: '请选择门店！', state: "No"}})
                                }
                            } else {
                                $('body').Overlays({data: {text: '请选择购置时间！', state: "No"}})
                            }
                        } else {
                            $('body').Overlays({data: {text: '请选择分公司！', state: "No"}})
                        }
                    } else {
                        $('body').Overlays({data: {text: '请输入已行驶里程！', state: "No"}})
                    }
                } else {
                    $('body').Overlays({data: {text: '请输入车辆名称！', state: "No"}})
                }
            })

            $('.CancelBut').on('click',function () {
                window.location.href=ctx+'/rentcar/list';
            })
        }
    });

    for(var i=0;i<$('input[type="number"]').length;i++){
        (function () {
            var index=i;
            $('input[type="number"]').eq(index).on('change',function () {
                if($(this).val()<0){
                    $(this).val(0)
                }
            })
        })()
    }
});